package org.racsor.cc.action;

import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.racsor.cc.model.PojoDamageBoss;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class CalculateDamageBossRequest extends HttpServlet {
    private static final Logger log = Logger.getLogger(CalculateDamageBossRequest.class.getName());

    public void doGet(HttpServletRequest req, HttpServletResponse resp) {

	try {
	    log.warning("CalculateDamageBossRequest:doGet");
	    RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/jsp/calculateDamageBoss.jsp");
	    dispatcher.forward(req, resp);

	} catch (Exception e) {
	    log.warning(ExceptionUtils.getStackTrace(e));
	}

    }

    public void doPost(HttpServletRequest req, HttpServletResponse resp) {

	try {
	    log.warning("CalculateDamageBossRequest:doPost");
	    PojoDamageBoss pojo = new PojoDamageBoss();

	    String name = (String) req.getParameter("name");
	    String level = (String) req.getParameter("level");
	    String skill = (String) req.getParameter("skill");
	    String attack = (String) req.getParameter("attack");
	    String defense = (String) req.getParameter("defense");
	    String building = (String) req.getParameter("building");
	    String staMax = (String) req.getParameter("staMax");
	    String sta = (String) req.getParameter("sta");

	    if (StringUtils.isNotBlank(name))
		pojo.setName(name);
	    if (StringUtils.isNumeric(level))
		pojo.setLevel(NumberUtils.toInt(level, 1));
	    if (StringUtils.isNumeric(skill))
		pojo.setSkill(NumberUtils.toInt(skill, 10));
	    if (StringUtils.isNumeric(attack))
		pojo.setAttack(NumberUtils.toInt(attack, 1));
	    if (StringUtils.isNumeric(defense))
		pojo.setDefense(NumberUtils.toInt(defense));
	    if (StringUtils.isNumeric(building))
		pojo.setBuilding(NumberUtils.toInt(building, 0));
	    if (StringUtils.isNumeric(staMax))
		pojo.setStaMax(NumberUtils.toInt(staMax, 1));
	    if (StringUtils.isNumeric(sta))
		pojo.setSta(NumberUtils.toInt(sta, 1));

	    int maxNatty = (int) (pojo.getLevel() + (pojo.getSkill() - 10) + (pojo.getAttack() + pojo.getDefense() - pojo.getBuilding()) / 6.045 + 450);
	    int minNatty = (int) (maxNatty * 0.95);
	    int maxSnake = (int) (pojo.getLevel() + (pojo.getSkill() - 10) + (pojo.getAttack() + pojo.getDefense() - pojo.getBuilding()) / 6.045 + 830);
	    int minSnake = (int) (maxSnake * 0.95);
	    int maxLoco = (int) (pojo.getLevel() + (pojo.getSkill() - 10) + (pojo.getAttack() + pojo.getDefense() - pojo.getBuilding()) / 6.045 + 900);
	    int minLoco = (int) (maxLoco * 0.95);
	    int maxPayne = (int) (pojo.getLevel() + (pojo.getSkill() - 10) + (pojo.getAttack() + pojo.getDefense() - pojo.getBuilding()) / 6.045 + 1990);
	    int minPayne = (int) (maxPayne * 0.95);

	    //min and max stamina needed for each boss
	    int leastNatty = (int) Math.ceil(100000.0 / maxNatty);
	    int mostNatty = (int) Math.ceil(100000.0 / minNatty);
	    int leastSnake = (int) Math.ceil(130000.0 / maxSnake);
	    int mostSnake = (int) Math.ceil(130000.0 / minSnake);
	    int leastLoco = (int) Math.ceil(275000.0 / maxLoco);
	    int mostLoco = (int) Math.ceil(275000.0 / minLoco);
	    int leastPayne = (int) Math.ceil(500000.0 / maxPayne);
	    int mostPayne = (int) Math.ceil(500000.0 / minPayne);

	    StringBuffer natty = new StringBuffer("<p style='color:");
	    StringBuffer snake = new StringBuffer("<p style='color:");
	    StringBuffer loco = new StringBuffer("<p style='color:");
	    StringBuffer payne = new StringBuffer("<p style='color:");

	    if (pojo.getSta() + pojo.getStaMax() < mostNatty) {
		natty.append("lightGray");
	    } else if (pojo.getSta() >= mostNatty) {
		natty.append("green");
	    } else if (pojo.getSta() >= leastNatty) {
		natty.append("blue");
	    } else {
		natty.append("red");
	    }
	    natty.append("'>");
	    natty.append(leastNatty + " - " + mostNatty);
	    natty.append("</p>");

	    if (pojo.getSta() + pojo.getStaMax() < mostSnake) {
		snake.append("lightGray");
	    } else if (pojo.getSta() >= mostSnake) {
		snake.append("green");
	    } else if (pojo.getSta() >= leastSnake) {
		snake.append("blue");
	    } else {
		snake.append("red");
	    }
	    snake.append("'>");
	    snake.append(leastNatty + " - " + mostSnake);
	    snake.append("</p>");

	    if (pojo.getSta() + pojo.getStaMax() < mostLoco) {
		loco.append("lightGray");
	    } else if (pojo.getSta() >= mostLoco) {
		loco.append("green");
	    } else if (pojo.getSta() >= leastLoco) {
		loco.append("blue");
	    } else {
		loco.append("red");
	    }
	    loco.append("'>");
	    loco.append(leastNatty + " - " + mostLoco);
	    loco.append("</p>");

	    if (pojo.getSta() + pojo.getStaMax() < mostPayne) {
		payne.append("lightGray");
	    } else if (pojo.getSta() >= mostPayne) {
		payne.append("green");
	    } else if (pojo.getSta() >= leastPayne) {
		payne.append("blue");
	    } else {
		payne.append("red");
	    }
	    payne.append("'>");
	    payne.append(leastPayne + " - " + mostPayne);
	    payne.append("</p>");

	    pojo.setNatty(natty.toString());
	    pojo.setSnake(snake.toString());
	    pojo.setLoco(loco.toString());
	    pojo.setPayne(payne.toString());

	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();

	    log.warning("user:" + user);

	    req.setAttribute("pojo", pojo);
	    log.warning("PojoDamageBoss:" + pojo.toString());

	    RequestDispatcher dispatcher = req.getRequestDispatcher("WEB-INF/jsp/calculateDamageBoss.jsp");
	    dispatcher.forward(req, resp);

	} catch (Exception e) {
	    log.warning(ExceptionUtils.getStackTrace(e));
	}

    }
}
